export default function createRouteMap (routes, oldPathList, oldPathMap) {
  // ['/', '/about']
  const pathList = oldPathList || []
  // { path: record, path: record  }
  const pathMap = oldPathMap || {}

  // 遍历所有的路由规则
  routes.forEach(route => {
    addRouteRecord(route, pathList, pathMap)
  })

  return {
    pathList,
    pathMap
  }
}

function addRouteRecord (route, pathList, pathMap, parentRecord) {
  // /about
  // /music/pop
  const path = parentRecord ? `${parentRecord.path}/${route.path}` : route.path

  const record = {
    path: path,
    component: route.component,
    parentRecord: parentRecord
  }

  if (!pathMap[path]) {
    pathList.push(path)
    pathMap[path] = record
  }

  // 判断当前route 是否有子路由
  if (route.children) {
    route.children.forEach(childRoute => {
      addRouteRecord(childRoute, pathList, pathMap, record)
    })
  }
}
